<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 */
class Fete_View_Helper_Link extends Fete_View_Helper
{
    public function link($params = array(), $routeName = 'default')
    {
        $request = Fete_Request::getInstance();
        $currentParams = $request->getAllParams();
        if ('default' === $routeName) {
            ob_start();
            $app        = isset($params['app'])
                ? $params['app'] : $request->getApp();
            $controller = isset($params['controller'])
                ? $params['controller'] : $request->getController();
            $action     = isset($params['action'])
                ? $params['action'] : $request->getAction();

            echo Fete_Kernel::getInstance()->getBaseUrl()
                , '/', $app, '/', $controller, '/', $action;

            unset($params['app'], $params['controller'], $params['action']);

            $params = array_merge($currentParams, $params);

            foreach ($params as $param => $value)
            {
                if (null !== $value) {
                    echo '/', $param, '/', $value;
                }
            }

            return ob_get_clean();
        }

        return $this->baseUrl() . '/'
        . $request->getRouteInstance($routeName)->getLink($params);
    }
}